package cn.dapeng.jdk21;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class VirtualThreadTest {

    public static void main(String[] args) throws Exception {
        ThreadLocal<String> t = new ThreadLocal<>();
        CustomThread customThread = new CustomThread(t);
        // 1. 启动方式一
        Thread.startVirtualThread(new Thread(customThread, "v_n")).join();
        // 2. 启动方式二
        Thread.ofVirtual().name("v_2").start(customThread).join();

        //3. 启动方式三
        Thread unStarted = Thread.ofVirtual().name("v_3").unstarted(customThread);
        unStarted.start();

        //4. 启动方式四，使用ThreadFactory创建(标识虚拟线程名)
        ThreadFactory factory = Thread.ofVirtual().name("v_4").factory();
        Thread thread = factory.newThread(customThread);
        thread.start();
        thread.join();

        //5. 使用Executors.newVirtualThreadPerTaskExecutor()创建
        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
        executor.submit(customThread);

        //6. 使用Executors.newThreadPerTaskExecutor()结合ThreadFactory创建
        ExecutorService executor2 = Executors.newThreadPerTaskExecutor(factory);
        executor2.submit(customThread);

        System.in.read();
    }
}
